import tensorflow as tf
from tensorflow import keras
import numpy as np

#https://blog.csdn.net/qq_35164554/article/details/90141461

imdb = keras.datasets.imdb
(train_data, train_labels), (test_data, test_labels) = imdb.load_data(num_words=10000)
print("Trraining entries:{},labels:{}".format(len(train_data), len(train_labels)))
print(train_data[0])

train_data = keras.preprocessing.sequence.pad_sequences(train_data,
                                                        value=0,
                                                        padding='post',
                                                        maxlen=256)

test_data = keras.preprocessing.sequence.pad_sequences(test_data,
                                                       value=0,
                                                       padding='post',
                                                       maxlen=256)
# print(len(train_data[0]), len(train_data[10]), len(train_data[100]))

# 构建模型
vocab_size = 10000

model = keras.Sequential()
model.add(keras.layers.Embedding(vocab_size, 16))
model.add(keras.layers.GlobalAveragePooling1D())
model.add(keras.layers.Dense(16, activation=tf.nn.relu))
model.add(keras.layers.Dense(1, activation=tf.nn.sigmoid))

model.summary()

# 损失函数和优化器
model.compile(optimizer='adam',
              loss='binary_crossentropy',
              metrics=['accuracy'])

"""
# 配置优化器
from keras import optimizers
model.compile(optimizer=optimizers.RMSprop(lr=0.001),
			 loss='binary_crossentropy',
			 metrics=['accuracy'])
"""

"""
# 使用自定义的损失和指标
from keras import losses
from keras import metrics
model.compile(optimizer=optimizers.RMSprop(lr=0.001),
			 loss=losses.binary_crossentropy,
			 metrics=[metrics.binary_accuracy])
"""

# 创建验证集
x_val = train_data[:10000]
partial_x_train = train_data[10000:]

y_val = train_labels[:10000]
partial_y_train = train_labels[10000:]

# 训练模型并且评估模型
history = model.fit(partial_x_train,
                    partial_y_train,
                    epochs=40,
                    batch_size=512,
                    validation_data=(x_val, y_val),
                    verbose=1)
results = model.evaluate(test_data, test_labels)

print("损失" + str(results[0]), "准确率" + str(results[1]))

# 保存模型
model.save("./models/imdbModel.h5")

json_string = model.to_json()
open('./models/imdbModel.json', 'w').write(json_string)  # 重命名

yaml_string = model.to_yaml()
open('./models/imdbModel.yaml', 'w').write(yaml_string)  # 重命名

"""

'''方法1：保存为json'''
json_string = model.to_json()
open('model_architecture_1.json', 'w').write(json_string)   #重命名

#从json中读出数据
from keras.models import model_from_json
model = model_from_json(json_string)
"""

"""
'''方法2：保存为yaml'''
yaml_string = model.to_yaml()
open('model_arthitecture_2.yaml', 'w').write(yaml_string)  #重命名

#从json中读出数据
from keras.models import model_from_yaml
model = model_from_yaml(yaml_string)
"""

# 绘制训练损失和验证损失
import matplotlib.pyplot as plt

history_dict = history.history
print("_______history_dict___________")
print(history_dict)

loss_values = history_dict['loss']
val_loss_values = history_dict['val_loss']
epochs = range(1, len(loss_values) + 1)
plt.plot(epochs, loss_values, 'mo--', label='Training loss')  # bo
plt.plot(epochs, val_loss_values, 'b.-', label='Validation loss')
plt.title('Training and validation loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.show()

# 绘制训练精度和验证精度
plt.clf()  # 清空图像
acc = history_dict['accuracy']
val_acc = history_dict['val_accuracy']
plt.plot(epochs, acc, 'mo-', label='Training acc')
plt.plot(epochs, val_acc, 'b.-', label='Validation acc')
plt.title('Training and validation accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend()
plt.show()


